﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Com;
using FwProject.Com.Dto;
using FwProject.Controllers;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Shop.Controllers
{
    public class RegionController : FwProjectControllerBase
    {

        private readonly IRegionAppService _regionAppService;

        public RegionController(IRegionAppService regionAppService)
        {
            _regionAppService = regionAppService;
        }
        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<JsonResult> GetRegionList(string parentid = null, string keyword = "")
        {
            var model = await _regionAppService.GetPagedListAsync(parentid, keyword);

            return Json(model);
        }



        public async Task<IActionResult> Create(string parentid = null)
        {
            var names = await _regionAppService.GetParentNamesAsync(parentid);
            ViewBag.PrentName = string.Join(">", names);

            return View(new RegionEnterDto { ParentId = parentid });
        }

        [HttpPost]
        public async Task<IActionResult> Create(RegionEnterDto input)
        {
            var id = await _regionAppService.CreateAsync(input);
            return Json(id);
        }

        public async Task<IActionResult> Edit(string id)
        {
            ViewBag.Id = id;
            var entity = await _regionAppService.GetEntityAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(string id,RegionEnterDto input)
        {
            await _regionAppService.ModifyAsync(id, input);
            return Json(id);
        }

        public async Task<IActionResult> Delete(string id)
        {
            await _regionAppService.DeleteAsync(id);
            return Json(id);
        }

        public async Task<IActionResult> Generate()
        {
            await _regionAppService.GenerateAsync();
            return Json("0");
        }
    }
}